home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_mysql.idb / usr / freeware / share / sql-bench / test-connect.z / test-connect
Text File  |  2002-10-07  |  9KB  |  293 lines

  1. #!/usr/sbin/perl
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18. #
  19. # This test is for testing the speed of connections and sending
  20. # data to the client.
  21. #
  22. # By changing the variable '$opt_loop_count' value you can make this test
  23. # easier/harderto your computer to execute. You can also change this value
  24. # by using option --loop_value='what_ever_you_like'.
  25. ##################### Standard benchmark inits ##############################
  26.  
  27. use DBI;
  28. use Benchmark;
  29.  
  30. $opt_loop_count=10000;    # Change this to make test harder/easier
  31. $str_length=65000;    # This is the length of blob strings in PART:5
  32. $max_test=20;        # How many times to test if the server is busy
  33.  
  34. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  35. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  36.  
  37. # This is the length of blob strings in PART:5
  38. $str_length=min($limits->{'max_text_size'},$limits->{'query_size'}-30,$str_length);
  39.  
  40. if ($opt_small_test)
  41. {
  42.   $opt_loop_count/=100;
  43. }
  44.  
  45. $opt_loop_count=min(1000, $opt_loop_count) if ($opt_tcpip);
  46.  
  47. print "Testing the speed of connecting to the server and sending of data\n";
  48. print "All tests are done $opt_loop_count times\n\n";
  49.  
  50. ################################# PART:1 ###################################
  51. ####
  52. ####  Start timeing and start connect test..
  53. ####
  54.  
  55. $start_time=new Benchmark;
  56.  
  57. print "Testing connection/disconnect\n";
  58.  
  59. $loop_time=new Benchmark;
  60. $errors=0;
  61.  
  62. for ($i=0 ; $i < $opt_loop_count ; $i++)
  63. {
  64.   print "$i " if (($opt_debug));
  65.   for ($j=0; $j < $max_test ; $j++)
  66.   {
  67.     if ($dbh = DBI->connect($server->{'data_source'}, $opt_user,
  68.                 $opt_password))
  69.     {
  70.       $dbh->disconnect;
  71.       last;
  72.     }
  73.     select(undef, undef, undef, 0.01*$j);
  74.     print "$errors " if (($opt_debug));
  75.     $errors++;
  76.   }
  77.   die "Got error '$DBI::errstr' after $i connects" if ($j == $max_test);
  78.   $dbh->disconnect;
  79.   undef($dbh);
  80. }
  81. $end_time=new Benchmark;
  82. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  83. print "Time to connect ($opt_loop_count): " .
  84.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  85.  
  86. ################################# PART:2 ###################################
  87. #### Now we shall do first one connect, then simple select
  88. #### (select 1..) and then close connection. This will be
  89. #### done $opt_loop_count times.
  90.  
  91. if ($limits->{'select_without_from'})
  92. {
  93.   print "Test connect/simple select/disconnect\n";
  94.   $loop_time=new Benchmark;
  95.  
  96.   for ($i=0; $i<$opt_loop_count; $i++)
  97.   {
  98.     $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password) || die $DBI::errstr;
  99.     $sth = $dbh->do("select 1") or die $DBI::errstr;
  100.     $dbh->disconnect;
  101.   }
  102.   $end_time=new Benchmark;
  103.   print "Time for connect+select_simple ($opt_loop_count): " .
  104.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  105. }
  106.  
  107. ################################# PART:3 ###################################
  108. ####
  109. #### Okay..Next thing we'll do is a simple select $opt_loop_count times.
  110. ####
  111.  
  112. $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password,
  113.             { PrintError => 0}) || die $DBI::errstr;
  114.  
  115. if ($limits->{'select_without_from'})
  116. {
  117.   print "Test simple select\n";
  118.   $loop_time=new Benchmark;
  119.   for ($i=0 ; $i<$opt_loop_count ; $i++)
  120.   {
  121.     $sth = $dbh->do("select 1") or die $DBI::errstr;
  122.   }
  123.   $end_time=new Benchmark;
  124.   print "Time for select_simple ($opt_loop_count): " .
  125.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  126. }
  127.  
  128. ################################# PART:4 ###################################
  129. #### First, we'll create a simple table 'bench1'
  130. #### Then we shall do $opt_loop_count selects from this table.
  131. #### Table will contain very simple data.
  132.  
  133. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'});
  134. do_many($dbh,$server->create("bench1",
  135.                  ["a int NOT NULL",
  136.                   "i int",
  137.                   "s char(10)"],
  138.                  ["primary key (a)"]));
  139. $sth = $dbh->do("insert into bench1 values(1,100,'AAA')") or die $DBI::errstr;
  140.  
  141. if ($opt_fast && defined($server->{vacuum}))
  142. {
  143.   $server->vacuum(0,\$dbh);
  144. }
  145. $dbh->disconnect;
  146.  
  147. #
  148. # First test connect/select/disconnect
  149. #
  150. print "Testing connect/select 1 row from table/disconnect\n";
  151.  
  152. $loop_time=new Benchmark;
  153. $errors=0;
  154.  
  155. for ($i=0 ; $i<$opt_loop_count ; $i++)
  156. {
  157.   for ($j=0; $j < $max_test ; $j++)
  158.   {
  159.     last if ($dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password));
  160.     $errors++;
  161.   }
  162.   die $DBI::errstr if ($j == $max_test);
  163.  
  164.   $sth = $dbh->do("select * from bench1") #Select * from table with 1 record
  165.     or die $DBI::errstr;
  166.   $dbh->disconnect;
  167. }
  168.  
  169. $end_time=new Benchmark;
  170. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  171. print "Time to connect+select_1_row ($opt_loop_count): " .
  172.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  173.  
  174. #
  175. # The same test, but without connect/disconnect
  176. #
  177. print "Testing select 1 row from table\n";
  178.  
  179. $dbh = $server->connect();
  180. $loop_time=new Benchmark;
  181.  
  182. for ($i=0 ; $i<$opt_loop_count ; $i++)
  183. {
  184.   $sth = $dbh->do("select * from bench1") # Select * from table with 1 record
  185.     or die $DBI::errstr;
  186. }
  187.  
  188. $end_time=new Benchmark;
  189. print "Time to select_1_row ($opt_loop_count): " .
  190.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  191.  
  192. #
  193. # The same test, but with 2 rows.
  194. #
  195. print "Testing select 2 rows from table\n";
  196.  
  197. $sth = $dbh->do("insert into bench1 values(2,200,'BBB')")
  198.   or die $DBI::errstr;
  199.  
  200. $loop_time=new Benchmark;
  201.  
  202. for ($i=0 ; $i<$opt_loop_count ; $i++)
  203. {
  204.   $sth = $dbh->do("select * from bench1") # Select * from table with 2 record
  205.     or die $DBI::errstr;
  206. }
  207.  
  208. $end_time=new Benchmark;
  209. print "Time to select_2_rows ($opt_loop_count): " .
  210.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  211.  
  212. if ($limits->{'functions'})
  213. {
  214.   print "Test select with aritmetic (+)\n";
  215.   $loop_time=new Benchmark;
  216.  
  217.   for ($i=0; $i<$opt_loop_count; $i++)
  218.   {
  219.     $sth = $dbh->do("select a+a+a+a+a+a+a+a+a+a from bench1") or die $DBI::errstr;
  220.   }
  221.   $end_time=new Benchmark;
  222.   print "Time for select_column+column ($opt_loop_count): " .
  223.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  224. }
  225.  
  226. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  227.   or die $DBI::errstr;
  228.  
  229. if ($opt_fast && defined($server->{vacuum}))
  230. {
  231.   $server->vacuum(0,\$dbh);
  232. }
  233.  
  234. ################################# PART:5 ###################################
  235. #### We'll create one table with a single blob field,but with a
  236. #### huge record in it and then we'll do $opt_loop_count selects
  237. #### from it.
  238.  
  239. goto skip_blob_test if (!$limits->{'working_blobs'});
  240.  
  241. print "Testing retrieval of big records ($str_length bytes)\n";
  242.  
  243. do_many($dbh,$server->create("bench1", ["b blob"], []));
  244. $dbh->{LongReadLen}= $str_length; # Set retrieval buffer
  245.  
  246. my $string=(A) x ($str_length); # This will make a string $str_length long.
  247. $sth = $dbh->prepare("insert into bench1 values(?)") or die $dbh->errstr;
  248. $sth->execute($string) or die $sth->errstr;
  249. undef($string);
  250. if ($opt_fast && defined($server->{vacuum}))
  251. {
  252.   $server->vacuum(0,\$dbh);
  253. }
  254.  
  255. $loop_time=new Benchmark;
  256.  
  257. for ($i=0 ; $i < $opt_loop_count ; $i++)
  258. {
  259.   $sth = $dbh->prepare("select * from bench1");
  260.   if (!$sth->execute || !(@row = $sth->fetchrow_array) ||
  261.       length($row[0]) != $str_length)
  262.   {
  263.     warn "$DBI::errstr - ".length($row[0])." - $str_length **\n";
  264.   }
  265.   $sth->finish;
  266. }
  267.  
  268. $end_time=new Benchmark;
  269. print "Time to select_big_str ($opt_loop_count): " .
  270.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  271.  
  272. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  273.   or do
  274. {
  275.     # Fix for Access 2000
  276.     die $dbh->errstr if (!$dbh->abort_if_fatal_error());
  277. };
  278.  
  279. if ($opt_fast && defined($server->{vacuum}))
  280. {
  281.   $server->vacuum(0,\$dbh);
  282. }
  283.  
  284. skip_blob_test:
  285.  
  286. ################################ END ###################################
  287. ####
  288. #### End of the test...Finally print time used to execute the
  289. #### whole test.
  290.  
  291. $dbh->disconnect;
  292. end_benchmark($start_time);
  293.